package com.example.algorithm.base;

import java.util.Arrays;

/**
 * @author: dangwen
 * @createDate: 2021/7/2
 */
public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {11, 22, 34, 45, 51};
        System.out.println(dome(arr,51));
    }
    public static int dome(int[] arr, int target){
        int left = 0;
        int right = arr.length - 1;
        while (left <= right){

            int mid = (left + right) / 2;
            if(arr[mid] == target){
                // mid
                return mid;
            }else if(arr[mid] < target){
                left = mid + 1;
            }else if(arr[mid] > target){
                right = mid - 1;
            }
        }
        return -1;
    }

}
